package sfsoftware.game.actor.enemy.bubblefish;

import sfsoftware.game.actor.attributes.Poisonable;
import sfsoftware.game.trevor.Enemy;
import sfsoftware.util.Clock;
import sfsoftware.util.Tile;
import sfsoftware.util.Updatable;
import sfsoftware.util.graphics.Graphics;

public class SimpleBubbleFish extends Enemy implements Updatable, Poisonable {

	protected Clock animationTimer = new Clock(500);
	protected boolean isPoisoned = false;	
		
	public SimpleBubbleFish(int destX, int destY, String texture, int destWidth, int destHeight, Tile[][] assocTileMap) {
		super(destX, destY, texture, 0, 0, destWidth, destHeight);	
		// Random starting direction
		direction = changeDirection();
				
		tileMap = assocTileMap;	
		health = 2;
		maxHealth = 2;
		
		setSpeed(2);
	}
	
	public void draw()
	{
		if(!isPoisoned)
		{
			super.draw(width, height, getSrcX(), getSrcY(), width, height, 1, 1, 1, 1);			
		}
		else
		{
			super.draw(width, height, getSrcX(), getSrcY(), width, height, 0, 1, 0, 1);
		}
		
		if(!(isInvulnerable()) && health > 0)
		{
			// Draw the translucent bubble around the bubblefish sprite.			
			Graphics.drawCircle(getX() + (getWidth() /2), getY() + (getHeight() /2), 20, 1, 1, 1, 0, 0.77f, 0.91f, 0.99f, 0.35f);
			Graphics.drawCircleOutline(getX() + (getWidth() /2), getY() + (getHeight() /2), 20, 0.14f, 0.29f, 0.41f, 1);			
		}
		else if(invulnerabilityDraw && health > 0)
		{
			Graphics.drawCircle(getX() + (getWidth() /2), getY() + (getHeight() /2), 20, 1, 1, 1, 0, 0.77f, 0.91f, 0.99f, 0.35f);
			Graphics.drawCircleOutline(getX() + (getWidth() /2), getY() + (getHeight() /2), 20, 0.14f, 0.29f, 0.41f, 1);
		}
	}	
	
	public boolean move(int dirX, int dirY, Tile[][] tileMap)
	{		
		boolean successfulMove = false;
		try
		{
			// Try to move!
			successfulMove = super.move(dirX, dirY, tileMap); 
			if(!successfulMove)
			{				
				// If the move was unsuccessful, change direction
				direction = changeDirection();
			}
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			// If the actor tried to move off the playing field, change direction!
			direction = changeDirection();
		}
		return successfulMove;
	}	
	
	private int changeDirection()
	{
		// If they are going up or down, they change direction to left or right
		if(direction == UP_DIRECTION  ||  direction == DOWN_DIRECTION)
		{
			 return (int)(Math.random() * 2) + 2;
		}
		// Otherwise, they change direction to up or down... 
		// ...makes them seem smarter than they are ;)
		return  (int)(Math.random() * 2);	
	}
	
	
	public void update()
	{
		if(!(isInvulnerable()))
		{
			switch(direction)
			{
				case DOWN_DIRECTION:
					this.move(0, 1, tileMap);
					break;
				case UP_DIRECTION:
					this.move(0, -1, tileMap);
					break;
				case RIGHT_DIRECTION:				
					this.move(1, 0, tileMap);
					break;
				case LEFT_DIRECTION:
					this.move(-1, 0, tileMap);
					break;				
			}
		}
		
		if(animationTimer.updateClock())
		{
			if(getSrcY() == 0)
			{
				setSrcY(40);
			}
			else
			{
				setSrcY(0);
			}
		}
	}

	public void setPoisoned(boolean p)
	{
		isPoisoned = p;
		// Being poisoned causes a character to move at half speed.
		if(isPoisoned)
		{
			speed = speed / 2;
		}
		else
		{
			speed = speed * 2;
		}
	}
	
	public void togglePoisoned()
	{
		if(this.isPoisoned)
		{
			setPoisoned(false);
		}
		else
		{
			setPoisoned(true);
		}
	}

}
